#include<iostream>
using namespace std;

  struct ListNode {
      int val;
      ListNode*next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode*next) : val(x), next(next) {}
  };


class Solution {
public:
    ListNode* dfs(ListNode* newhead)
    {

        if (newhead==nullptr||newhead->next == nullptr)
            return newhead;

       ListNode*newhead1=dfs(newhead->next);
        newhead->next->next = newhead;
        newhead->next = nullptr;
        
        return newhead1;
    }
    ListNode* reverseList(ListNode* head) {
        return dfs(head);
    }
};

int main()
{
    ListNode* head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
    Solution s;
    s.reverseList(head);



    return 0;
}